package com.gegao.practise;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class TestTimer extends Activity {

    Button mBt1;
    Button mBt2;
    
    Timer mTimer;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.two_button);
        
        mBt1 = (Button)findViewById(R.id.bt1);
        mBt1.setText("link");
        
        mBt2 = (Button)findViewById(R.id.bt2);
        mBt2.setText("lamp");
        
        
        
        mBt1.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                if(null != mTimer){
                    mTimer.cancel();
                }
                noticeUserAfterClick();
            }
        });
        
        mBt2.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                if(null != mTimer){
                    mTimer.cancel();
                }
            }
        });
    }
    
    private void noticeUserAfterClick(){
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            
            @Override
            public void run() {
                TestTimer.this.runOnUiThread(new Runnable() {
                    
                    @Override
                    public void run() {
                        Toast toast = Toast.makeText(TestTimer.this, "click the lamp !!!", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 80); 
                        toast.show();
                    }
                });
                
            }
        }, 1000);
    }

}
